<template>
  <div style="">
    <el-dialog v-model="editQuestionDialog" title="修改题目" width="500px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="题目题干" prop="title">
          <el-input type="textarea" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="所属题组" prop="questionGroupId">
          <el-select v-model="form.questionGroupId" placeholder="请选择题组">
            <el-option v-for="(group,index) in groups" :key="index" :label="group.groupName"
                       :value="group.questionGroupId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目类型" prop="type">
          <el-select v-model="form.type">
            <el-option label="单选" :value="0"></el-option>
            <el-option label="多选" :value="1"></el-option>
            <el-option label="判断" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <div v-if="form.type !== 2">
          <el-form-item
            v-for="(option, index) in form.options"
            :key="index"
            :label="'选项 ' + (index + 1)"
          >
            <el-input v-model="option.content"></el-input>
            <el-button size="small" type="primary" @click.prevent="removeOption(option)">删除</el-button>
            &nbsp;
            <el-checkbox v-model="form.answers[index]" :label="index" size="large">正确答案</el-checkbox>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item>
            <el-radio-group v-model="form.answers[0]">
              <el-radio :label="false">错</el-radio>
              <el-radio :label="true">对</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <el-form-item v-if="form.type !== 2">
          <el-button size="small" type="primary" @click.prevent="addOption()">新增</el-button>
        </el-form-item>
        <el-form-item label="分数">
          <el-input-number v-model="form.score"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :rows="2" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="editQuestionDialog = false">取消</el-button>
        <el-button type="primary" @click="doSubmit" :loading="loading">确认</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
  <el-button size="small" type="primary" @click="editQuestionDialog = true">修改</el-button>
</template>

<script lang="ts" setup>
import { defineProps, inject, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { editQuestion } from '@/api/admin'

interface Option {
  content: string,
  order: number
}

const reload: any = inject('reload')
const loading = ref<boolean>(false)
const editQuestionDialog = ref<boolean>(false)
const props = defineProps<{
  question: any,
  groups: any
}>()
const answers: boolean[] = []
// eslint-disable-next-line vue/no-setup-props-destructure
const list: Array<string> = JSON.parse(JSON.stringify(props.question.answer))
if (props.question.type === 2) {
  answers.push(list[0] !== '0')
} else {
  for (let i = 0; i <= Number(list[list.length - 1]); i++) {
    answers.push(list.indexOf(i.toString()) !== -1)
  }
}
const form = reactive<{
  questionId: number,
  title: string,
  remark: string,
  questionGroupId: number,
  score: number,
  options: Option[],
  type: number,
  answers: boolean[]
}>({
  questionId: props.question.questionId,
  title: props.question.title,
  remark: props.question.remark,
  questionGroupId: props.question.questionGroupId,
  score: props.question.score,
  options: JSON.parse(JSON.stringify(props.question.options)),
  type: props.question.type,
  answers: answers
})
const doSubmit = () => {
  form.options.forEach(option => {
    option.order = form.options.indexOf(option)
  })
  loading.value = true
  editQuestion(form).then(() => {
    ElMessage.success('修改成功')
    editQuestionDialog.value = false
    reload()
  }).finally(() => {
    loading.value = false
  })
}
const removeOption = (item: Option) => {
  const index = form.options.indexOf(item)
  if (index !== -1) {
    form.options.splice(index, 1)
    form.answers.splice(index, 1)
  }
}
const addOption = () => {
  form.options.push({
    content: '',
    order: 0
  })
  form.answers.push(false)
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 10px;
}
</style>
